package cn.neusoft.controller;

import cn.neusoft.po.Permission;
import cn.neusoft.query.PermissionQuery;
import cn.neusoft.service.IPermissionService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxj
 * @since 2023-12-19
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    IPermissionService service;

    @PostMapping
    public ResponseObj saveOrUpdate(@RequestBody Permission permission){
        boolean b = service.saveOrUpdate(permission);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(10001,"保存权限信息失败");
    }

    @DeleteMapping("/{id}")
    public ResponseObj delete(@PathVariable("id") Integer id){
        boolean b = service.removeById(id);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(10002,"删除权限信息失败");
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Permission permission = service.getById(id);
        return ResponseObj.SUCCESS(permission);
    }

    @PostMapping("/page")
    public ResponseObj getByPage(@RequestBody PermissionQuery query){
        QueryWrapper queryWrapper = new QueryWrapper();
        if (query.getName()!=null){
            queryWrapper.like("name",query.getName());
        }
        Page pageQuery = new Page(query.getCurrent(),query.getSize());
        Page<Permission> page = service.page(pageQuery, queryWrapper);
        return ResponseObj.SUCCESS(page);
    }

    @GetMapping("/all")
    public ResponseObj getAll(){
        List<Permission> list = service.list();
        return ResponseObj.SUCCESS(list);
    }

    @GetMapping("/byRoleId/{rid}")
    public ResponseObj getByRoleId(@PathVariable("rid") Integer roleId){
        List<Permission> permissionList = service.getByRoleId(roleId);
        return ResponseObj.SUCCESS(permissionList);
    }
    
}
